﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using iTextSharp.text.html.simpleparser;
using System.Net.Mail;
using System.Net;


namespace DataAccess
{
    public class FaultRepository : ConnectionClass
    {

        public FaultRepository() : base () {}

        public void AddFaultDetails(string username, int productID,string faultDetail)
        {
            UserProductFault upf = new UserProductFault();
            upf.UserName = username;
            upf.ProductID = productID;
            upf.FaultDetail = faultDetail;
            upf.FaultID = 1;

            db.AddToUserProductFaults(upf);
            db.SaveChanges();
        }

        public IEnumerable<UserProductFault> ListOfItemFaults(int ProductID,DateTime startdate,DateTime endDate,string username)
        {
            return (from upf in db.UserProductFaults
                    where upf.ProductID == ProductID && upf.Date >= startdate && upf.Date <= endDate && upf.UserName == username
                    select upf);
        }

        public IEnumerable<UserProductFault> getAllFaults()
        {
            return db.UserProductFaults;
        }

        public UserProductFault getFaultByID(int id)
        {
            return db.UserProductFaults.SingleOrDefault(x => x.ID == id);
        }

        public UserProductFault getFaultByID(int id, Guid orderID)
        {
            return db.UserProductFaults.SingleOrDefault(x => x.ID == id && x.OrderID == orderID);
        }

        public IEnumerable<Fault> GetAllFaultStatus()
        {
            return db.Faults;
        }

        public Fault getFaultStatusByID(int id)
        {
            return db.Faults.SingleOrDefault(x => x.ID == id);
        }

        public void addFaultLog(FaultLog fault)
        {
            db.FaultLogs.AddObject(fault);
            db.SaveChanges();
        }

        public void UpdateFault(UserProductFault fault)
        {
            UserProductFault o = getFaultByID(fault.ID);
            db.UserProductFaults.Attach(o);
            db.UserProductFaults.ApplyCurrentValues(fault);
            db.SaveChanges();
        }

        public string CreatePDFFault(UserProductFault fault)
        {

            User user = new UserRepository().GetUserByUserName(fault.UserName);

            string contents = "";
            Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("C:\\Users\\Andrea\\Desktop\\" + fault.ID + ".pdf", FileMode.Create));
            doc.Open();
            contents = "<b>Fault Receipt</b><br/><br/>";
            contents += "<table><tr><td><b>Username:</b>" + user.UserName + "</td></tr><tr><td><b>Full Name:</b>" + user.FirstName + " " + user.LastName + "</td></tr><tr><td><b>Address:</b>" +user.HouseNo+" "+ user.StreetName+ ", " + user.TownName + ", " + user.Country.CountryName + "</td></tr><tr><td><b>Date:</b>" + fault.Date + "</td></table>";
            contents += "<br/>";
            contents += "<table><b><tr><td>Fault Id</td><td>Product Name</td><td>Fault Description</td></tr></b>";

            Product p = new ProductRepository().GetProductByID((int)fault.ProductID);

            contents += "<tr><td>" + fault.ID + "</td><td>" + p.Name + "</td><td>" + fault.Fault.Description + "</td></tr>";

            contents += "</table>";

            //Read string contents using stream reader and convert html to parsed conent
            var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);

            //Get each array values from parsed elements and add to the PDF document
            foreach (var htmlElement in parsedHtmlElements)
                doc.Add(htmlElement as IElement);

            doc.Close();

            string location = "C:\\Users\\Andrea\\Desktop\\" + fault.ID + ".pdf";

            return location;
        }
    }
}
